<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/5/27
 * Time: 11:16
 */

namespace app\index\controller;


use app\admin\model\AssembleList;
use app\admin\model\AssembleUser;
use app\admin\model\CityRange;
use app\admin\model\GoodsList;
use app\admin\model\OrderList;
use app\index\model\JsApiTicket;
use app\admin\model\WechatConfig;
use think\facade\Session;
use think\Db;

class Assemble extends Base
{



    public function assembleInfo($id){
        $ass = AssembleList::where('id',$id)->find();
        $good = GoodsList::where('id',$ass['good_id'])->find();
        $good['images'] = explode(',',$good['images']);
        $good['range_id'] = CityRange::where('id','in',$good['range_id'])->column('name');
        $users = AssembleUser::where('good_id',$ass['good_id'])->select()->each(function ($item){
            $item->user = \app\admin\model\User::where('id',$item->user_id)->field('id,nickname,headimgurl')->find();
        })->toArray();
        if (!$users){
            $users = 0;
        }
        $tel = WechatConfig::where('wx_id',1)->value('phone');
        $this->assign('tel',$tel);
        $this->assign('ass',$ass);
        $this->assign('good',$good);
        $this->assign('users',$users);
        return $this->fetch();
    }

    public function assShare($id){
        $users = AssembleUser::where('id',$id)->select()->each(function ($item){
            $item->user = \app\admin\model\User::where('id',$item->user_id)->field('id,nickname,headimgurl')->find();
            $item->goods = GoodsList::where('id',$item->good_id)->field('id,good_name,image')->find();
            $item->ass = AssembleList::where('good_id',$item->good_id)->field('id,number,a_price')->find();
        })->toArray();
        $user_id = Session::get('user_id');
        $order = OrderList::where('f_id',$users[0]['order_id'])->where('user_id',$user_id)->find();
        $type = $user_id==$users[0]['user']['id']?1:2;
        if ($order){
            $type = 3;
        }
        if ($users[0]['end_time'] <= time()){
            $type = 4;
        }
        $whereAFa['wx_id']=1;
        $wechatRes=Db::name('wechatconfig')->where($whereAFa)->find();
        $this->assign("wechatRes",$wechatRes);

        header("Content-Type:text/html;charset=utf-8");
        define('WX_APPID',$wechatRes['appid']);
        define('WX_MCHID',$wechatRes['mch_id']);
        define('WX_KEY',$wechatRes['paykey']);
        define('WX_APPSECRET',$wechatRes['appsecret']);

        //分享传值
        $wechat=new JsApiTicket();
        $config=$wechat->getSignPackage();

        $this->assign('appid',$config['appId']);
        $this->assign('timestamp',$config['timestamp']);
        $this->assign('noncestr',$config['nonceStr']);
        $this->assign('signature',$config['signature']);
        $url='http://'.$_SERVER['HTTP_HOST'];
        $this->assign('url',$url.'/index/assemble/assShare?id='.$id);
        $this->assign('title',$users[0]['goods']['good_name']);
        $this->assign('imgUrl',$url.$users[0]['goods']['image']);
        $this->assign('data',$users[0]);
        $this->assign('type',$type);
        return $this->fetch();
    }

    public function userInfo($id,$type){
        $this->assign('ass_id',$id);
        $this->assign('type',$type);
        return $this->fetch();
    }



    public function orderDown(){
        $post = input('post.');
        $validate = $this->validate($post,'app\index\validate\User');
        if (true !== $validate){
            return json(['code'=>0,'msg'=>$validate]);
        }
        if($post['code']!=cookie('code')){
            return json(['code'=>0,'msg'=>'验证码错误！']);
        }
        if ($post['type']== 2){
            $data = $this->order($post);
            if ($data == 2){
                return json(['code'=>0,'msg'=>'参团失败，人数已满']);
            }
        }else{
            $find = AssembleList::where('id',$post['ass_id'])->find();
            $good = GoodsList::where('id',$find['good_id'])->find();
            if(!$good){
                return json(['code'=>0,'msg'=>'商品不存在']);
            }
            $data=[
                'order_no'=>$this->randomStr().time(),
                'user_id'=>Session::get('user_id'),
                'phone'=>$post['phone'],
                'true_name'=>$post['true_name'],
                'good_id'=>$good['id'],
                'good_name'=>$good['good_name'],
                'image'=>$good['image'],
                'sign_type'=>$good['driver_id'],
                'pay_money'=>$find['a_price'],
                'pay_type'=>0,
                'test_type'=>1,
                'create_time'=>time(),
                'order_type'=>2,
                'id_card'=>$post['id_card']
            ];
        }

        $save=OrderList::insertGetId($data);
        if($save){
            cookie('code',null);
            \app\admin\model\User::update(['phone'=>$post['phone'],'id_card'=>$post['id_card'],'true_name'=>$post['true_name']],['id'=>$find['id']]);
            $url='/index/wxpay/goodsPay?id='.$save.'&type=1';
            return json(['code'=>1,'msg'=>'提交成功！','jump_url'=>$url]);
        }
        return json(['code'=>0,'msg'=>'提交失败！']);
    }


    private function order($post){
        $user = AssembleUser::where('id',$post['ass_id'])->find();
        $find = AssembleList::where('good_id',$user['good_id'])->find();
        $good = GoodsList::where('id',$find['good_id'])->find();
        if(!$good){
            return json(['code'=>0,'msg'=>'商品不存在']);
        }
        if ($user['number'] == 0){
            return 2;
        }
        $data=[
            'order_no'=>$this->randomStr().time(),
            'f_id'=>$user['order_id'],
            'user_id'=>Session::get('user_id'),
            'phone'=>$post['phone'],
            'true_name'=>$post['true_name'],
            'good_id'=>$good['id'],
            'good_name'=>$good['good_name'],
            'image'=>$good['image'],
            'sign_type'=>$good['driver_id'],
            'pay_money'=>$find['a_price'],
            'pay_type'=>0,
            'test_type'=>1,
            'create_time'=>time(),
            'order_type'=>2,
            'id_card'=>$post['id_card']
        ];
        return $data;
    }




}